package com.scs.application.modules.doc.controller;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.scs.application.core.aliyun.oss.handler.AliyunOssHandler;
import com.scs.application.core.model.CommonResult;
import com.scs.application.core.mvc.controller.BaseController;
import com.scs.application.modules.doc.dto.FileUploadDTO;
import com.scs.application.modules.doc.entity.FileInfo;
import com.scs.application.modules.doc.entity.FileUpload;
import com.scs.application.modules.doc.service.FileInfoService;
import com.scs.application.modules.doc.service.FileUploadService;
import lombok.AllArgsConstructor;
import org.springframework.web.bind.annotation.*;

import javax.servlet.ServletException;
import java.io.IOException;
import java.util.List;

/**
 * @Description:
 */
@AllArgsConstructor
@RestController
@RequestMapping("/doc/files")
public class FileController extends BaseController {


    private final FileInfoService fileInfoService;
    private final FileUploadService fileUploadService;

    private final AliyunOssHandler aliyunOssHandler;

//    @PostMapping("/upload")
//    public CommonResult singleUpload(@RequestParam("file") MultipartFile file,
//                                     @RequestParam(name = "bizType", required = false) String bizType,
//                                     @RequestParam(name = "bizKey", required = false) String bizKey,
//                                     @RequestParam(name = "tags", required = false) String tags) {
//        if (file == null) {
//            return CommonResult.error("上传失败,请选择需要上传的文件");
//        }
//
//        FileInfo fileInfo = fileInfoService.uploadFile(file, bizType, bizKey);
//        if (fileInfo == null) {
//            return CommonResult.error("上传失败");
//        }
//
//        return CommonResult.ok("上传成功", fileInfo);
//    }

    @PostMapping("/upload")
    public CommonResult singleUpload(FileUploadDTO dto) {
        if (dto.getFile() == null) {
            return CommonResult.error("上传失败,请选择需要上传的文件");
        }

        FileInfo fileInfo = fileInfoService.uploadFile(dto);
        if (fileInfo == null) {
            return CommonResult.error("上传失败");
        }

        return CommonResult.ok("上传成功", fileInfo);
    }

    @GetMapping("/{fileId}")
    public void download(@PathVariable("fileId") String fileId,
                         @RequestParam(value = "preview", required = false) String preview,
                         @RequestParam(value = "x-oss-process", required = false) String style) throws IOException, ServletException {
        FileInfo info = fileInfoService.getById(fileId);
        if (info == null) {
            return;
        }
//        Date expiration = new Date(System.currentTimeMillis() + 3600 * 1000);
//        String signUrl = aliyunOssHandler.getSignUrl(info.getPath(), expiration, style);
//
//        response().sendRedirect(signUrl);
    }

    /**
     * 查询文件信息
     *
     * @param bizType
     * @param bizKey
     * @return
     * @throws IOException
     */
    @GetMapping("/list")
    public CommonResult list(@RequestParam(name = "bizType", required = false) String bizType,
                             @RequestParam(name = "bizKey", required = false) String bizKey) throws IOException {
        List<FileUpload> list = fileUploadService.list(Wrappers.<FileUpload>query().eq("biz_key", bizKey).eq("biz_type", bizType));
        return CommonResult.ok("查询成功", list);
    }


}
